﻿using System;
using System.Configuration;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Netkk.Common.Interfaces.Services;
using Netkk.Core;
using Netkk.Core.Auth;
using Netkk.Library;

namespace Netkk
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        private static IUnityContainer container;

        public static IUnityContainer Container
        {
            get { return container; }
        }


        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            SetuupControllerFactory();
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.ashx");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = ""
                }  // Parameter defaults
            );
        }

        private void SetuupControllerFactory()
        {
            container = new UnityContainer();

            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers["containerOne"].Configure(container);

            IControllerFactory controllerFactory = new UnityControllerFactory(container);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);

            Application["container"] = container;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            ISendmailService email = container.Resolve<ISendmailService>();
            email.Sendmail(
                new MailAddress("info@dotnetkk.de"),
                "Error on DOTNETKK Website", 
                Server.GetLastError().ToString() );
        }

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            string name = String.Empty;
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                name = authTicket.Name;
            }
            var identity = new CustomIdentity(name, "Forms");
            var principal = new CustomPrincipal(identity);
            Context.User = principal;

            Sitemap.Create("~/App_Data/Menu.xml");
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {

        }
    }
}